package com.gx.smart.building.ui.inspection.fragment

import android.content.Intent
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.TimeUtils
import com.gx.smart.base.BaseAdapter
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentInspectionCompleteBinding
import com.gx.smart.building.ui.common.timepicker.TimePicker
import com.gx.smart.building.ui.inspection.InspectionTaskCompleteActivity
import com.gx.smart.building.ui.inspection.adapter.InspectionRecordAdapter
import com.gx.smart.building.ui.inspection.event.TaskCompleteEvent
import com.gx.smart.lib.http.api.response.PlanTask
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskDto
import org.greenrobot.eventbus.EventBus
import razerdp.basepopup.QuickPopupBuilder
import razerdp.basepopup.QuickPopupConfig

class InspectionCompleteFragment : CommonFragment(), BaseAdapter.OnItemClickListener<PlanTask> {
    private lateinit var viewBinding: FragmentInspectionCompleteBinding
    private lateinit var adapter: InspectionRecordAdapter
    private var handleTimeStart: Long = 0L
    private var handleTimeEnd: Long = 0L
    override fun onBindLayout(): Int = R.layout.fragment_inspection_complete

    override fun onItemClick(e: PlanTask, position: Int) {
        val intent = Intent(requireActivity(), InspectionTaskCompleteActivity::class.java)
        intent.putExtra("id", e.id)
        startActivity(intent)
        EventBus.getDefault().postSticky(TaskCompleteEvent(e))
    }

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentInspectionCompleteBinding.inflate(layoutInflater, root, true)
        viewBinding.filter.setOnClickListener {
            QuickPopupBuilder.with(context)
                .contentView(R.layout.inspection_popwindow_layout)
                .config(
                    QuickPopupConfig()
                        .gravity(Gravity.BOTTOM or Gravity.CENTER_VERTICAL)
                        .withClick(
                            R.id.all,
                            { v1: View? ->
                                all()
                            }, true
                        )
                        .withClick(
                            R.id.time,
                            { v1: View? ->
                                timePicker()
                            }, true
                        )
                )
                .show(it)
        }
    }

    private fun all() {
        viewBinding.filterContent.text = ""
        handleTimeStart = 0L
        handleTimeEnd = 0L
        if (this::viewBinding.isInitialized) {
            viewBinding.refreshLayout.autoRefresh()
        }
    }

    private fun timePicker() {
        val deadlinePicker = TimePicker(
            requireContext()
        ) { startTime, endTime ->
            viewBinding.filterContent.text = TimeUtils.millis2String(
                startTime,
                "yyyy-MM-dd"
            ) + "-" + TimeUtils.millis2String(endTime, "yyyy-MM-dd")
            handleTimeStart = startTime
            handleTimeEnd = endTime
            if (this::viewBinding.isInitialized) {
                viewBinding.refreshLayout.autoRefresh()
            }
        }
        deadlinePicker.setIsLoop(false)
        deadlinePicker.setTouchMode(true)
        deadlinePicker.setMode(TimePicker.MODE.YMD)
        deadlinePicker.show()
    }

    override fun initContent() {
        super.initContent()
        adapter = InspectionRecordAdapter(activity)
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun doAction() {
        viewModel.queryPlanTaskList(currentPage, 1, keyword ?: "", handleTimeStart, handleTimeEnd)
            .observe(viewLifecycleOwner) {
                handleResult(it)
            }
    }

    override fun search(keyword: String?) {
        super.search(keyword)
        if (this::viewBinding.isInitialized) {
            viewBinding.refreshLayout.autoRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        viewBinding.refreshLayout.autoRefresh()
    }


    companion object {
        @JvmStatic
        fun newInstance() = InspectionCompleteFragment()
    }
}